from collections import OrderedDict

from django import http
from django.shortcuts import render
from django.views import View

from apps.goods.models import GoodsCategory, SKU


class ShucaiView(View):
    """蔬菜商品页"""

    def get(self, request):
        """提供商品列表页"""

        # good_category=GoodsCategory.objects.order_by('parent_id')
        goods = SKU.objects.filter(category_id=2).all()

        #
        hots=SKU.objects.filter(category_id=2).order_by('-sales').all()
        print(hots)
        context = {
            'goods': goods,
            'hots':hots
        }
        # good_dict={'good':good}

        return render(request, 'shucai.html',context=context)


class FrultView(View):
    '''水果商品页'''

    def get(self,request):
        # categories_dict = OrderedDict()  # 创建一个有序的字典（先添加的key-value在前面）

        # good_category=GoodsCategory.objects.order_by('parent_id')
        goods = SKU.objects.filter(category_id=1).all()

        #

        context = {
            'goods':goods
        }
        # good_dict={'good':good}


        return render(request, 'fruit.html',context=context)

class DetailView(View):
    """商品详情页"""

    def get(self, request,sku_id):
        """提供商品详情页"""

        try:
            sku=SKU.objects.get(id=sku_id)
        except Exception as e:
            return http.HttpResponseBadRequest('商品id错误')
        # print(sku.category_id)
        hot_id=sku.category_id
        goods=sku.spu.sku_set.all()
        # print(goods)
        hots=SKU.objects.filter(category_id=hot_id).order_by('-sales').all()
        print(hots)

        #
        context={
            'sku':sku,
            'goods':goods,
            'hots':hots,

        }
        #
        # print(context)
        return render(request, 'detail.html',context=context)

